' navbar.bmx

Strict

Import "host.bmx"
Import "nodeview.bmx"
Import "style.bmx"

Type TNavBar Extends TEventHandler
	Field	host:THost'Editor
	Field	tabber:TGadget
	Field	viewlist:TList=New TList
	Field	selected:TNodeView
	Field	navmenu:TGadget
	Field	navstyle:TGadgetStyle
'	Field	options:TEditorOptions
	
	Method SelectedView()
		If selected Return selected.viewindex
	End Method
		
	Method SelectView(index)
		Local	n:TNodeView
'		Print "TNavBar.SelectView:"+index
'		If index>=viewlist.count() Return
		n=TNodeView(viewlist.ValueAtIndex(index))
		If Not n Notify "selectview failed";Return
		If n<>selected
			If selected HideGadget selected.treeview
			selected=n
		EndIf
		ShowGadget n.treeview
		SelectGadgetItem tabber,index
	End Method
	
	Method AddView(node:TDataNode)
		Local	nodeview:TNodeView
		Local	index,root:TGadget
		
		For nodeview=EachIn viewlist
			If nodeview.root=node
				SelectView nodeview.viewindex
				Return
			EndIf
		Next
		
		nodeview=TNodeView.CreateNodeView(tabber,node)
'		nodeview.owner=Self
		
		If navstyle navstyle.Apply nodeview.treeview
		SetGadgetLayout nodeview.treeview,1,1,1,1
		HideGadget nodeview.treeview

		viewlist.AddLast nodeview
		nodeview.viewindex=viewlist.count()-1

		AddGadgetItem tabber,node.name
		
		root=TreeViewRoot(nodeview.treeview)
		node.setnode root,nodeview.viewindex

'		SelectView nodeview.index
		Return nodeview.viewindex
	End Method

	Function CreateNodeView:TNodeView(group:TGadget,root:TDataNode,index)
		Local	nodeview:TNodeView
		nodeview=New TNodeView
		nodeview.root=root
		nodeview.treeview=CreateTreeView(0,0,ClientWidth(group),ClientHeight(group),group)
		nodeview.viewindex=index
		Return nodeview
	End Function	

	Method OnEvent()
		If EventSource()=tabber
			SelectView SelectedGadgetItem(tabber)				
		End If
		If selected And EventSource()=selected.treeview
'			If selected.viewindex DebugStop
			selected.OnEvent
		EndIf
	End Method

	Method Refresh()
		Local	view:TNodeView
		For view=EachIn viewlist
			If navstyle navstyle.Apply view.treeview
		Next
	End Method

	Method Invoke(command$,argument:Object=Null)
		If command="REFRESH" Refresh()		
'		If command="NEWVIEW" And selected selected.NewView
	End Method

	Function CreateNavMenu:TGadget()
		Local	edit:TGadget
		edit=CreateMenu("&Nav",0,Null)
		CreateMenu "&New View",MENUNEWVIEW,edit
		Return edit
	End Function

	Function CreateNavBar:TNavBar(host:THost,parent:TGadget,style:TGadgetStyle)
		Local	n:TNavBar
		n=New TNavBar
		n.host=host
'		n.options=TEditorOptions(host.editoroptions)'Requester(host.options)	
		n.navstyle=style		'host.GetStyle("navstyle")
		n.tabber=CreateTabber(0,0,1,1,parent)
'		n.AddView root
		n.navmenu=CreateNavMenu()
		Return n		
	End Function

End Type


